package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import com.etoak.common.core.exception.CustomException;
import com.etoak.common.core.vo.PageVO;
import com.etoak.entity.Ingredients;
import com.etoak.mapper.IngredientsMapper;
import com.etoak.service.DishesService;
import com.etoak.service.IngredientsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.constraints.NotBlank;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author etoak
 * @since 2024-12-18
 */
@Service
public class IngredientsServiceImpl extends ServiceImpl<IngredientsMapper, Ingredients> implements IngredientsService {

    @Autowired
    DishesService dishesService;

    @Override
    public void add(Ingredients ingredients) {
        /* 食材名称不能为空 */
        if (ObjectUtils.isNotEmpty(getByName(ingredients.getName()))) {
            throw new CustomException("食材名称重复!");
        }

        ingredients.setCreateTime(DateUtil.now());
        this.save(ingredients);
    }

    @Override
    public PageVO<Ingredients> getList(int pageNum, int pageSize, Ingredients ingredients) {
        PageHelper.startPage(pageNum, pageSize);

        //List<Ingredients> ingredientsList = lambdaQuery().likeRight(StringUtils.isNotEmpty(ingredients.getName()), Ingredients::getName, ingredients.getName())
        //        .eq(StringUtils.isNotEmpty(ingredients.getType()), Ingredients::getType, ingredients.getType())
        //        .orderByDesc(Ingredients::getCreateTime)
        //       .list();

        List<Ingredients> ingredientsList = baseMapper.getList(ingredients);
        PageInfo<Ingredients> pageInfo = new PageInfo<>(ingredientsList);
        return new PageVO<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void update(int id, Ingredients ingredients) {
        /* 判断食材是否为空, 为空就不更新了 */
        if (ObjectUtils.isEmpty(getById(id))) {
            throw new CustomException("食材不存在!");
        }

        /* 判断食材的名称
        *  - 如果没有, 直接更新
        *  - 如果有
        *   1. 查询到的食材id和要更新的id不一致, 直接返回
        *   2. 如果一致则更新
        * */
        String ingredientsName = ingredients.getName();
        if (StringUtils.isNotEmpty(ingredientsName)) {
            Ingredients savedIngredients = getByName(ingredientsName);
            if (ObjectUtils.isNotEmpty(savedIngredients) && !savedIngredients.getId().equals(id)) {
                throw new CustomException("食材名称重复!");
            }
        }

        ingredients.setId(id);
        ingredients.setUpdateTime(DateUtil.now());
        this.updateById(ingredients);

    }

    @Override
    public void delete(int id) {
        /* 判断食材是否为空, 为空就不更新了 */
        if (ObjectUtils.isEmpty(getById(id))) {
            throw new CustomException("食材不存在!");
        }

        if (dishesService.isIngredientsUsed(id)) {
            throw new CustomException("食材使用中, 不允许删除!");
        }

        this.removeById(id);
    }

    @Override
    public List<Ingredients> listByType(String type) {
        Ingredients ingredients = new Ingredients();
        ingredients.setType(type);

        return baseMapper.getList(ingredients);
    }

    private Ingredients getByName(String name) {
        return lambdaQuery().eq(Ingredients::getName, name).one();
    }
}
